package com.kaesun.ufit.weight;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.kaesun.ufit.hardware.Buzzer;
import com.kaesun.ufit.tools.ConfigData;
import com.kaesun.ufit.tools.ModelList;
import com.kaesun.ufit.tools.RepeatListener;
import com.kaesun.ufit.tools.ToolbarView;

public class MachineActivity extends Activity implements View.OnClickListener, IOnHandlerMessage {

	private static final int BUTTON_IDS[] = {
		R.id.button_back, R.id.button_home,

		R.id.text_machine_select,
		/*R.id.button_number_left, R.id.button_number_right,
		R.id.button_weight_left, R.id.button_weight_right,
		R.id.button_count_left, R.id.button_count_right,*/

		R.id.button_ok
	};

	private static final int NUMBER_MIN = 1;
	private static final int NUMBER_MAX = 9;
	private static final int COUNT_MIN = 8;
	private static final int COUNT_MAX = 40;

	private MainApplication mApp = null;
	private WeakRefHandler mHandler = null;
	private ToolbarView mToolbarView = null;

	private TextView mTextViewMachine = null;
	private TextView mTextViewNumber = null;
	private TextView mTextViewWeight = null;
	private TextView mTextViewCount = null;

	private int mIdx = 0;
	private String mMachine = "";
	private int mNumber = 1;
	private int mWeight = 5;
	private int mCount = 16;

	private long mElapsedTime = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_machine);

		for (int i=0; i<BUTTON_IDS.length; i++) {
			findViewById(BUTTON_IDS[i]).setOnClickListener(this);
		}

		findViewById(R.id.button_number_left).setOnTouchListener(new RepeatListener(1000, 150, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeNumber(-1);
			}
		}));
		findViewById(R.id.button_number_right).setOnTouchListener(new RepeatListener(1000, 150, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeNumber(1);
			}
		}));

		findViewById(R.id.button_weight_left).setOnTouchListener(new RepeatListener(1000, 150, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeWeight(-1);
			}
		}));
		findViewById(R.id.button_weight_right).setOnTouchListener(new RepeatListener(1000, 150, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeWeight(1);
			}
		}));

		findViewById(R.id.button_count_left).setOnTouchListener(new RepeatListener(1000, 150, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeCount(-1);
			}
		}));
		findViewById(R.id.button_count_right).setOnTouchListener(new RepeatListener(1000, 150, new OnClickListener() {
			@Override
			public void onClick(View v) {
				changeCount(1);
			}
		}));

		mApp = (MainApplication)getApplication();
		mHandler = new WeakRefHandler((IOnHandlerMessage)this);
		mToolbarView = (ToolbarView)findViewById(R.id.view_toolbar);

		mTextViewMachine = (TextView)findViewById(R.id.text_machine_select);
		mTextViewNumber = (TextView)findViewById(R.id.text_machine_number);
		mTextViewWeight = (TextView)findViewById(R.id.text_machine_weight);
		mTextViewCount = (TextView)findViewById(R.id.text_machine_count);

		mToolbarView.chage2Button();
		mToolbarView.setTitle("Machine");

		mIdx = ConfigData.getPrefInteger(this, ConfigData.PREF_MODEL_IDX, ConfigData.DEF_MODEL_IDX);
		mMachine = ModelList.itemsName(mIdx);
		mNumber = ConfigData.getPrefInteger(this, ConfigData.PREF_MODEL_NO, ConfigData.DEF_MODEL_NO);
		mWeight = ConfigData.getPrefInteger(this, ConfigData.PREF_MODEL_BARBEL_WEIGHT, ConfigData.DEF_MODEL_BARBEL_WEIGHT);
		mCount = ConfigData.getPrefInteger(this, ConfigData.PREF_MODEL_BARBEL_COUNT, ConfigData.DEF_MODEL_BARBEL_COUNT);

		mTextViewMachine.setText(mMachine);
		changeNumber(0);
		changeWeight(0);
		changeCount(0);
	}

	@Override
	protected void onDestroy() {

		super.onDestroy();
	}

	@Override
	protected void onPause() {

		super.onPause();
	} 

	@Override
	protected void onResume() {
		mApp.setHandler(mHandler, true);
		mElapsedTime = mApp.rtcGetTimeInMillis();

		super.onResume();
	}

	public void onClick(View arg0) {
		mApp.timeoutReset();

		Buzzer.delay(50);
		switch (arg0.getId()) {
		case R.id.button_back:
			setResult(RESULT_OK);
			finish();
			break;
		case R.id.button_home:
			setResult(RESULT_CANCELED);
			finish();
			break;

		case R.id.text_machine_select:
			if (1000 < (mApp.rtcGetTimeInMillis() - mElapsedTime)) {
				mElapsedTime = mApp.rtcGetTimeInMillis();

				machineDialog();
			}
			break;

		case R.id.button_ok:
			update();
			setResult(RESULT_CANCELED);
			finish();
			break;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mApp.timeoutReset();

		return super.onTouchEvent(event);
	}

	@Override
	public void handleMessage(Message msg) {
		switch (msg.what) {
		case MainApplication.HANDLER_TIMEOUT_SECOND:
			mToolbarView.setTime(msg.arg1, msg.arg2);
			break;
		case MainApplication.HANDLER_TIMEOUT_ACTIVITY:
			setResult(RESULT_OK);
			finish();
			break;
		}
	}


	private int selIndex = -1;
	private void machineDialog() {
		selIndex = -1;
		String[] items = new String[ModelList.itemsLength()];
		for (int i=0; i<ModelList.itemsLength(); i++) {
			if (mIdx == ModelList.itemsIdx(i)) {
				selIndex = mIdx;
			}
			items[i] = ModelList.itemsName(i);
		}

		new AlertDialog.Builder(this)
		.setTitle("Select of machine")
		.setSingleChoiceItems(items, selIndex, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				selIndex = which;
			}
		})
		.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (0 != mMachine.compareTo(ModelList.itemsName(selIndex))) {

					mIdx = selIndex;
					mMachine = ModelList.itemsName(selIndex);
					mNumber = 1;
					mWeight = ModelList.itemsBarbelWeight(selIndex);
					mCount = ModelList.itemsBarbelCount(selIndex);

					mTextViewMachine.setText(mMachine);
					changeNumber(0);
					changeWeight(0);
					changeCount(0);
				}
			}
		})
		.setNegativeButton("Cancel", null)
		.show();
	}

	private void changeNumber(int offset) {
		mNumber += offset;
		if (NUMBER_MIN > mNumber) {
			mNumber = NUMBER_MAX;
		} else if (NUMBER_MAX < mNumber) {
			mNumber = NUMBER_MIN;
		}
		mTextViewNumber.setText(String.format("%d", mNumber));
	}

	private void changeWeight(int offset) {
		if (0 != offset) {
			mWeight = (5 == mWeight) ? 10 : 5;
		}
		mTextViewWeight.setText("" + mWeight + " KG");
	}

	private void changeCount(int offset) {
		mCount += offset;
		if (COUNT_MIN > mCount) {
			mCount = COUNT_MAX;
		} else if (COUNT_MAX < mCount) {
			mCount = COUNT_MIN;
		}
		mTextViewCount.setText("" + mCount);
	}

	private void update() {
		ConfigData.setPref(this, ConfigData.PREF_MODEL_IDX, mIdx);
		ConfigData.setPref(this, ConfigData.PREF_MODEL_NO, mNumber);
		ConfigData.setPref(this, ConfigData.PREF_MODEL_NEO_ID, (ModelList.itemsNeoId(mIdx) + mNumber));
		ConfigData.setPref(this, ConfigData.PREF_MODEL_MOMENTUM_START, ModelList.itemsMomentumStart(mIdx));
		ConfigData.setPref(this, ConfigData.PREF_MODEL_MOMENTUM_RANGE, ModelList.itemsMomentumRange(mIdx));
		ConfigData.setPref(this, ConfigData.PREF_MODEL_BARBEL_WEIGHT, mWeight);
		ConfigData.setPref(this, ConfigData.PREF_MODEL_BARBEL_COUNT, mCount);
		ConfigData.setPref(this, ConfigData.PREF_MODEL_HALL_GAP, ModelList.itemsHallGap(mIdx));
		ConfigData.setPref(this, ConfigData.PREF_MODEL_HALL_COUNT, ModelList.itemsHallCount(mIdx));
	}
}
